#include "sparse_io.hh"


boost::shared_ptr< sp_stat_mat_t >
read_pairs (istream& FIN, bool weighted)
{
  int u,v; double w;
  boost::shared_ptr< sp_stat_mat_t > out_pt( new sp_stat_mat_t );
  sp_stat_mat_t& out = *(out_pt.get());
  if(!weighted)
    {
      while( FIN >> u >> v )
	{
	  if( u < 0 || v < 0 )
	    {
#ifdef DEBUG
	      cerr << "ignore negative dimension" << endl;
#endif
	      continue;
	    }
	  if( u == v )
	    {
#ifdef DEBUG
	      cerr << "ignore self-loop" << endl;
#endif
	      continue;
	    }
	  out(dim_t(u),dim_t(v),1.);
	  out(dim_t(v),dim_t(u),1.);
	}
    }
  else
    {
      while( FIN >> u >> v >> w)
	{
	  if( u < 0 || v < 0 )
	    {
#ifdef DEBUG
	      cerr << "ignore negative dimension" << endl;
#endif
	      continue;
	    }
	  if( u == v )
	    {
#ifdef DEBUG
	      cerr << "ignore self-loop" << endl;
#endif
	      continue;
	    }
	  out(dim_t(u),dim_t(v),w);
	  out(dim_t(v),dim_t(u),w);
	}
    }
  return out_pt;
}

boost::shared_ptr< sp_stat_mat_t >
read_pairs (const char* fname, bool weighted)
{
  ifstream fin( fname, ios::in );
  boost::shared_ptr< sp_stat_mat_t > ret = read_pairs ( fin, weighted );
  fin.close();
  return ret;
}

void
write_pairs (const sp_stat_mat_t& mat, ostream& FOUT)
{
  for(sp_stat_mat_t::row_iterator_t r=mat.begin_row();
      r!=mat.end_row(); ++r)
    {
      for( sp_stat_mat_t::col_iterator_t c = mat.begin_col(dim_t(*r));
	   c != mat.end_col(dim_t(*r)); ++c )
	FOUT << (*r) << "\t" << c.dim() << "\t" << c.value() << endl;
    }
}

void
write_pairs (const sp_stat_mat_t& mat, const char* fname)
{
  ofstream fout( fname, ios::out );
  write_pairs( mat, fout );
  fout.close();
}


// EOF
